<template>
	<view class="contont">
		<nav-bar :title="title" :leftIcon="true" />
		<view class="content-bgc">
			<view class="b-con">
				<up-cell-group :border="false">
					<up-cell title="生产计划单号">
						<template #value>
							<view class="cell-text">{{ detail_record.code }} </view>
						</template>
					</up-cell>
					<up-cell title="客户">
						<template #value>
							<view class="cell-text">{{ detail_record.client_name }} </view>
						</template>
					</up-cell>
					<up-cell title="交货日期">
						<template #value>
							<view class="cell-text">{{ detail_record.delivery_date }}</view>
						</template>
					</up-cell>
					<up-cell title="预计完成日期">
						<template #value>
							<view class="cell-text">{{ detail_record.finish_date }}</view>
						</template>
					</up-cell>
					<up-cell title="备注">
						<template #value>
							<view class="cell-text" style="width: 70%;">{{ detail_record.desc }}</view>
						</template>
					</up-cell>
				</up-cell-group>
			</view>
			<view class="b-con">
				<up-cell-group :border="false">
					<up-cell :isLink="true" :border="false" :title="`产品/半成品：总数量（${process_list.length}）`"
						@click="car_popupShow = true">
						<template #value>
							<view class="cell-text">点击查看 </view>
						</template>
					</up-cell>
				</up-cell-group>
			</view>
			<custom-popup :popupShow="car_popupShow" mode="bottom" :title="'生产'" @close="car_popupShow = false" height="70vh">
				<z-paging v-if="car_popupShow" ref="paging_car" :refresher-enabled="false" :loading-more-enabled="false" :fixed="false" v-model="car_list"
					@query="queryList_car">
					<template #top>
						<view style="background-color: #fff;">
							<up-tabs :list="list" lineWidth="60" @change="tab_change" :scrollable="false"
								style="border-bottom: 1rpx solid #e9e9e9;"></up-tabs>
							<up-tabs :list="list2" lineWidth="20" @change="tab_change2" :scrollable="false"
								style="border-bottom: 1rpx solid #e9e9e9;" v-show="uTab1 == '1'"></up-tabs>
							<view v-show="uTab1 == '1'">
							</view>
						</view>
					</template>
					<uni-card v-for="(item, index) in car_list" :key="index" margin="5px" spacing="0" :title="item.name">
						<view v-show="uTab1 == '1'">
							<view class="ctent">规格：{{ item.specification }}</view>
							<view class="ctent">BOM量：{{ item.bom_num }}</view>
							<view class="ctent">生产数量：{{ item.num }}</view>
						</view>

						<view v-show="uTab1 == '2'">
							<view class="con">
								<view class="ctent">单次工价(元)：{{ item.price }}</view>
								<view class="ctent">单次完成时间(秒)：{{ item.finish_m }}</view>
							</view>
							<view style="background-color: #f2f5f9;">
								<uni-card v-for="val in item.children" margin="5px" spacing="0" :title="val.name">
									<view class="ctent">单次工价(元)：{{ val.price }}</view>
									<view class="ctent">单次完成时间(秒)：{{ val.finish_m }}</view>
									<view class="ctent">工序数量：{{ val.now_num }}</view>
									<view class="ctent">工序要求：<view class="white-text">{{ val.require }}</view>
									</view>
								</uni-card>
							</view>
						</view>
					</uni-card>
				</z-paging>
			</custom-popup>
		</view>
	</view>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import pPlanApi from '../../../api/production/p_plan'

const detail_record = ref({})
const title = '生产计划详情';
onLoad(option => {
	get_detail(option.id);
})

const list = [
	{ name: '生产产品(步骤一)', status: "1" },
	{ name: '生产工序(步骤二)', status: '2' }
]

const list2 = [
	{ name: '产品', status: "1" },
	{ name: '半成品', status: '2' }
]
const uTab1 = ref('1')
const uTab2 = ref('1')

const pro_list = ref([])
const bom_list = ref([])
const process_p = ref([])
const process_h = ref([])
const process_list = ref([])
const car_popupShow = ref(false)

function get_detail(id) {
	pPlanApi.production_plan_detail(id).then(data => {
		detail_record.value = data
		for (let i = 0; i < data.plan_products.length; i++) {
			let bom_lists = null
			if (data.plan_products[i].p_type === 2) {
				bom_lists = data.plan_products[i].product.children
			} else {
				bom_lists = JSON.parse(data.plan_products[i].product.bom_info)['bom_info']
			}
			const bom_num = bom_lists.reduce((total, item) => {
				return total + (Number(item.num) || 0)
			}, 0)
			const info = {
				id: data.plan_products[i].product.id,
				code: data.plan_products[i].product.code,
				name: data.plan_products[i].product.name,
				specification: data.plan_products[i].product.specification,
				bom_num: bom_num,
				bom_info: data.plan_products[i].product.bom_info,
				unit_main: data.plan_products[i].product.unit_main,
				num: data.plan_products[i].num,
				f_num: bom_num * data.plan_products[i].num,
				process_data: data.plan_products[i].product.process_data
			}
			if (data.plan_products[i].p_type === 2) {
				bom_list.value.push(info)
			} else {
				pro_list.value.push(info)
			}

			const plan_products_process = data.plan_products[i].plan_products_process
			let all_price = 0
			let all_finish_m = 0
			const children_list = []
			for (let j = 0; j < plan_products_process.length; j++) {
				all_price += plan_products_process[j].process.price
				all_finish_m += plan_products_process[j].process.finish_m

				children_list.push({
					id: plan_products_process[j].process.id,
					id_info: 'process_' + plan_products_process[j].process.id,
					type: 'process',
					name: plan_products_process[j].process.name,
					price: plan_products_process[j].process.price,
					finish_m: plan_products_process[j].process.finish_m,
					now_num: plan_products_process[j].num,
					require: plan_products_process[j].process.require
				})
			}
			if (data.plan_products[i].p_type === 2) {
				process_h.value.push({
					id: data.plan_products[i].product.id,
					code: data.plan_products[i].product.code,
					id_info: 'bom_' + data.plan_products[i].product.id,
					type: 'bom',
					name: data.plan_products[i].product.name,
					price: all_price,
					finish_m: all_finish_m,
					now_num: '-', // 总体 now_num 不在这层处理
					require: '-',
					children: children_list
				})
			} else {
				process_p.value.push({
					id: data.plan_products[i].product.id,
					code: data.plan_products[i].product.code,
					id_info: 'product_' + data.plan_products[i].product.id,
					type: 'product',
					name: data.plan_products[i].product.name,
					price: all_price,
					finish_m: all_finish_m,
					now_num: '-', // 总体 now_num 不在这层处理
					require: '-',
					children: children_list
				})
			}
			process_list.value = [...process_h.value, ...process_p.value]
		}
	})
}

const car_list = ref([])
const paging_car = ref(null)
function tab_change(v) {
	uTab1.value = v.status
	paging_car.value.reload();
}
function tab_change2(v) {
	uTab2.value = v.status
	paging_car.value.reload();
}
function queryList_car() {
	if (uTab1.value == '1') {
		if (uTab2.value == '1') {
			paging_car.value.complete(pro_list.value);
		}
		if (uTab2.value == '2') {
			paging_car.value.complete(bom_list.value);
		}
	}
	if (uTab1.value == '2') {
		paging_car.value.complete(process_list.value);
	}
}

</script>

<style lang="scss"></style>